package com.element.music.controller;

import com.element.music.component.cofig.BaseResponse;
import com.element.music.controller.query.user.*;
import com.element.music.controller.vo.UserIdVO;
import com.element.music.service.UserService;
import com.element.music.service.dto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 功能描述：用户的controller
 * <p>
 * User: nft musician   developer
 * Date: 2022/1/10
 */

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    private static BaseResponse successResponse() {
        return BaseResponse.of().setCode(0).setMsg("edit user information successfully");
    }

    @PostMapping("add")
    public BaseResponse addUser(@RequestParam("walletAddress") String walletAddress) {
        UserIdVO userIdVO = userService.addUser(walletAddress);
        return BaseResponse.of().setCode(0).setMsg("create user successfully").setData(userIdVO);
    }


    @PostMapping("edit/username")
    public BaseResponse editUserName(@RequestBody UserQueryDTO userQueryDTO) {
        userService.updateUserName(userQueryDTO.getUserId(), userQueryDTO.getUserName());
        return successResponse();
    }

    @PostMapping("edit/email")
    public BaseResponse editUserEmail(@RequestBody UserQueryDTO userQueryDTO) {
        userService.updateUserEmail(userQueryDTO.getUserId(), userQueryDTO.getEmail());
        return successResponse();
    }

    @PostMapping("edit/profile_img")
    public BaseResponse editProfileImg(@RequestBody UserProfileImgQueryDTO userProfileImgQueryDTO) {
        userService.updateUserProfileImg(userProfileImgQueryDTO.getUserId(), userProfileImgQueryDTO.getProfileImg());
        return successResponse();
    }

    @PostMapping("edit/backdrop")
    public BaseResponse editBackDrop(@RequestBody UserBackDropQueryDTO userBackDropQueryDTO) {
        userService.updateUserBackDrop(userBackDropQueryDTO.getUserId(), userBackDropQueryDTO.getBackdrop());
        return successResponse();
    }

    @PostMapping("edit/twitter")
    public BaseResponse editTwitter(@RequestBody UserQueryDTO userQueryDTO) {
        userService.updateUserTwitter(userQueryDTO.getUserId(), userQueryDTO.getTwitter());
        return successResponse();
    }

    @PostMapping("edit/website")
    public BaseResponse editWebSite(@RequestBody UserQueryDTO userQueryDTO) {
        userService.updateUserWebsite(userQueryDTO.getUserId(), userQueryDTO.getWebsite());
        return successResponse();
    }


    @PostMapping("login")
    public BaseResponse loginUser(@RequestParam("walletAddress") String walletAddress) {
        UserDTO userDTO = userService.login(walletAddress);
        return BaseResponse.of().setCode(0).setMsg("user login successfully").setData(userDTO);
    }


    @PostMapping("logout")
    public BaseResponse logoutUser(@RequestParam("walletAddress") String walletAddress) {
        userService.logout(walletAddress);
        return BaseResponse.of().setCode(0).setMsg("user logout successfully");
    }


    @GetMapping("view/wallet_address")
    public BaseResponse getUserByWalletAddress(@RequestParam("walletAddress") String walletAddress) {
        UserDTO userDTO = userService.getUserByWalletAddress(walletAddress);
        return BaseResponse.of().setCode(0).setMsg("view user information successfully").setData(userDTO);
    }

    @GetMapping("view/id")
    public BaseResponse getUserByUserId(@RequestParam("id") Long userId) {
        UserDTO userDTO = userService.getUserById(userId);
        return BaseResponse.of().setCode(0).setMsg("view user information successfully").setData(userDTO);
    }

    @GetMapping("list")
    public BaseResponse getUserList() {
        List<UserDTO> userDTOList = userService.getUserList();
        return BaseResponse.of().setCode(0).setMsg("list user successfully").setData(userDTOList);
    }

    @DeleteMapping("delete")
    public BaseResponse deleteUserByWalletAddress(@RequestParam("walletAddress") String walletAddress) {
        return BaseResponse.of().setCode(0).setMsg("delete user successfully");
    }

}
